package com.yesjpt.serv.impl;

import java.util.List;


import com.yesjpt.serv.BookServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.yesjpt.dao.BookDao;
import com.yesjpt.domain.book;

@Service
class BookServImpl implements BookServ {
    @Autowired
    private BookDao dao;

    public book findByName(String bookName) {
        System.out.println(new Throwable().getStackTrace()[0] + "，正在根据name得到city信息");
        return dao.findByName(bookName);
}

    @Override
    @Transactional
    //写入数据库的时候，要加@Transactional注解，理解事务
    public JSONObject deleteById(String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        Integer r = dao.deleteById(id);
        if (r > 0) {
            o.put("success", true);
            o.put("message", "删除操作成功");
        } else {
            o.put("success", false);
            o.put("message", "删除操作失败，没有这个记录，id = " + id);
        }
        return o;
    }

    @Override
    @Transactional
    public JSONObject add(book vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", true);
        try {
            dao.add(vo);
            o.put("message", "新增操作成功");
        } catch (Exception e) {
            o.put("success", true);
            o.put("message", "新增操作异常：" + e);
        }
        return o;
    }

    @Override
    public book findById(String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return dao.findById(id);
    }

    @Override
    public List<book> listAll() {
        System.out.println(new Throwable().getStackTrace()[0]);
        return dao.listAll();
    }

    @Override
    @Transactional
    public JSONObject update(book vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", true);
        try {
            dao.update(vo);
            o.put("message", "更新操作成功");
            return o;
        } catch (Exception e) {
            o.put("success", true);
            o.put("message", "更新操作异常：" + e);
            return o;
        }
    }

}
